home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_resources.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  17.5 KB  |  496 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct ResourcesCallbackUserData {
  31.     APTR ud_List;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG reslist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->pool, msg->entry, sizeof(struct ResourceEntry));
  38. }
  39.  
  40. MakeHook(reslist_con2hook, reslist_con2func);
  41.  
  42. static __asm __saveds LONG reslist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->pool, &msg->entry);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(reslist_des2hook, reslist_des2func);
  50.  
  51. static __asm __saveds LONG reslist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     struct ResourceEntry *re = (struct ResourceEntry *)msg->entry;
  54.  
  55.     if (re) {
  56.         msg->strings[0] = re->re_Address;
  57.         msg->strings[1] = re->re_Name;
  58.         msg->strings[2] = re->re_Type;
  59.         msg->strings[3] = re->re_Pri;
  60.         msg->strings[4] = re->re_Version;
  61.         msg->strings[5] = re->re_OpenCount;
  62.         msg->strings[6] = re->re_RAMPtrCount;
  63.     } else {
  64.         msg->strings[0] = "Address";
  65.         msg->strings[1] = "ln_Name";
  66.         msg->strings[2] = "ln_Type";
  67.         msg->strings[3] = "ln_Pri";
  68.         msg->strings[4] = "Version";
  69.         msg->strings[5] = "OCnt";
  70.         msg->strings[6] = "RPC";
  71.         msg->preparses[0] = MUIX_B;
  72.         msg->preparses[1] = MUIX_B;
  73.         msg->preparses[2] = MUIX_B;
  74.         msg->preparses[3] = MUIX_B;
  75.         msg->preparses[4] = MUIX_B;
  76.         msg->preparses[5] = MUIX_B;
  77.         msg->preparses[6] = MUIX_B;
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83. MakeHook(reslist_dsp2hook, reslist_dsp2func);
  84.  
  85. static LONG reslist_cmp2colfunc( struct ResourceEntry *re1,
  86.                                  struct ResourceEntry *re2,
  87.                                  ULONG column )
  88. {
  89.     LONG pri1, pri2;
  90.  
  91.     switch (column) {
  92.         case 0: return stricmp(re1->re_Address, re2->re_Address);
  93.         case 1: return stricmp(re1->re_Name, re2->re_Name);
  94.         case 2: return stricmp(re1->re_Type, re2->re_Type);
  95.         case 3: IsDec(re1->re_Pri, &pri1); IsDec(re2->re_Pri, &pri2); return pri2 - pri1;
  96.         case 4: return stricmp(re1->re_Version, re2->re_Version);
  97.         case 5: return stricmp(re1->re_OpenCount, re2->re_OpenCount);
  98.         case 6: return stricmp(re1->re_RAMPtrCount, re2->re_RAMPtrCount);
  99.     }
  100. }
  101.  
  102. static __asm __saveds LONG reslist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
  103. {
  104.     LONG cmp;
  105.     struct ResourceEntry *re1, *re2;
  106.     ULONG col1, col2;
  107.  
  108.     re1 = (struct ResourceEntry *)msg->entry1;
  109.     re2 = (struct ResourceEntry *)msg->entry2;
  110.     col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
  111.     col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  112.  
  113.     if (msg->sort_type == MUIV_NList_SortType_None) return 0;
  114.  
  115.     if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
  116.         cmp = reslist_cmp2colfunc(re2, re1, col1);
  117.     } else {
  118.         cmp = reslist_cmp2colfunc(re1, re2, col1);
  119.     }
  120.  
  121.     if (cmp != 0 || col1 == col2) return cmp;
  122.  
  123.     if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
  124.         cmp = reslist_cmp2colfunc(re2, re1, col2);
  125.     } else {
  126.         cmp = reslist_cmp2colfunc(re1, re2, col2);
  127.     }
  128.  
  129.     return cmp;
  130. }
  131.  
  132. MakeHook(reslist_cmp2hook, reslist_cmp2func);
  133.  
  134. static BOOL TestResource( struct Library *lib )
  135. {
  136.     if (stricmp(lib->lib_Node.ln_Name, FSRNAME) == 0 ||
  137.         stricmp(lib->lib_Node.ln_Name, CARDRESNAME) == 0 ||
  138.         stricmp(lib->lib_Node.ln_Name, KEYMAPRESNAME) == 0) {
  139.         return FALSE;
  140.     } else {
  141.         return TRUE;
  142.     }
  143. }
  144.  
  145. static void ReceiveList( void (* callback)( struct ResourceEntry *re, void *userData ),
  146.                          void *userData )
  147. {
  148.     struct ResourceEntry *re;
  149.  
  150.     if (re = tbAllocVecPooled(globalPool, sizeof(struct ResourceEntry))) {
  151.         if (SendDaemon("GetResList")) {
  152.             while (ReceiveDecodedEntry((UBYTE *)re, sizeof(struct ResourceEntry))) {
  153.                 callback(re, userData);
  154.             }
  155.         }
  156.  
  157.         tbFreeVecPooled(globalPool, re);
  158.     }
  159. }
  160.  
  161. static void IterateList( void (* callback)( struct ResourceEntry *re, void *userData ),
  162.                          void *userData )
  163. {
  164.     struct Library *res;
  165.     struct MinList tmplist;
  166.     struct ResourceEntry *re, *_re;
  167.  
  168.     NewList((struct List *)&tmplist);
  169.  
  170.     Forbid();
  171.  
  172.     ITERATE_LIST(&SysBase->ResourceList, struct Library *, res) {
  173.         if (re = AllocVec(sizeof(struct ResourceEntry), MEMF_PUBLIC)) {
  174.             ULONG cnt;
  175.  
  176.             re->re_Addr = res;
  177.  
  178.             _snprintf(re->re_Address, sizeof(re->re_Address), "$%08lx", res);
  179.             stccpy(re->re_Name, res->lib_Node.ln_Name, sizeof(re->re_Name));
  180.             _snprintf(re->re_Pri, sizeof(re->re_Pri), "%4ld", res->lib_Node.ln_Pri);
  181.  
  182.             if (strcmp(re->re_Name, FSRNAME) == 0 || strcmp(re->re_Name, KEYMAPRESNAME) == 0) {
  183.                 stccpy(re->re_Version, "---", sizeof(re->re_Version));
  184.             } else {
  185.                 _snprintf(re->re_Version, sizeof(re->re_Version), "%ld.%ld", res->lib_Version, res->lib_Revision);
  186.             }
  187.  
  188.             if (TestResource(res)) {
  189.                 _snprintf(re->re_OpenCount, sizeof(re->re_OpenCount), "%6lD", res->lib_OpenCnt);
  190.  
  191.                 cnt = GetRamPointerCount(res);
  192.                 if (cnt == (res->lib_NegSize / LIB_VECTSIZE)) {
  193.                     stccpy(re->re_RAMPtrCount, "***", sizeof(re->re_RAMPtrCount));
  194.                 } else {
  195.                     _snprintf(re->re_RAMPtrCount, sizeof(re->re_RAMPtrCount), "%4lD", cnt);
  196.                 }
  197.             } else {
  198.                 stccpy(re->re_OpenCount, "---", sizeof(re->re_OpenCount));
  199.                 stccpy(re->re_RAMPtrCount, "---", sizeof(re->re_RAMPtrCount));
  200.             }
  201.  
  202.             stccpy(re->re_Type, GetNodeType(res->lib_Node.ln_Type), sizeof(re->re_Type));
  203.  
  204.             AddTail((struct List *)&tmplist, (struct Node *)re);
  205.         }
  206.     }
  207.  
  208.     Permit();
  209.  
  210.     ITERATE_CHANGING_LIST(&tmplist, struct ResourceEntry *, re, _re) {
  211.         callback(re, userData);
  212.         FreeVec(re);
  213.     }
  214. }
  215.  
  216. static void UpdateCallback( struct ResourceEntry *re,
  217.                             void *userData )
  218. {
  219.     struct ResourcesCallbackUserData *ud = (struct ResourcesCallbackUserData *)userData;
  220.  
  221.     InsertSortedEntry(ud->ud_List, re);
  222.     ud->ud_Count++;
  223. }
  224.  
  225. static void PrintCallback( struct ResourceEntry *re,
  226.                            void *userData )
  227. {
  228.    PrintFOneLine((BPTR)userData, " %s %4s %4s %4s %s %s\n", re->re_Address, re->re_Pri, re->re_OpenCount, re->re_RAMPtrCount, re->re_Name, re->re_Version);
  229. }
  230.  
  231. static void SendCallback( struct ResourceEntry *re,
  232.                           void *userData )
  233. {
  234.     SendEncodedEntry((UBYTE *)re, sizeof(struct ResourceEntry));
  235. }
  236.  
  237. static ULONG __saveds mNew( struct IClass *cl,
  238.                             Object *obj,
  239.                             struct opSet *msg )
  240. {
  241.     APTR reslist, restext, rescount, updateButton, printButton, removeButton, priorityButton, moreButton, funcButton, exitButton;
  242.  
  243.     if (obj = (Object *)DoSuperNew(cl, obj,
  244.         MUIA_HelpNode, ResourcesText,
  245.         MUIA_Window_ID, MakeID('R','E','S','O'),
  246.         WindowContents, VGroup,
  247.  
  248.             Child, reslist = MyNListviewObject(MakeID('R','S','L','V'), "BAR,BAR,BAR P=" MUIX_C ",BAR P=" MUIX_R ",BAR P=" MUIX_C ",BAR P=" MUIX_R ",BAR P=" MUIX_R "", &reslist_con2hook, &reslist_des2hook, &reslist_dsp2hook, &reslist_cmp2hook, TRUE),
  249.             Child, MyBelowListview(&restext, &rescount),
  250.  
  251.             Child, MyVSpace(4),
  252.  
  253.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  254.                 Child, priorityButton = MakeButton(txtPriority),
  255.                 Child, removeButton   = MakeButton(txtRemove),
  256.                 Child, funcButton     = MakeButton(txtFunctions),
  257.             End,
  258.  
  259.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  260.                 Child, updateButton   = MakeButton(txtUpdate),
  261.                 Child, printButton    = MakeButton(txtPrint),
  262.                 Child, moreButton     = MakeButton(txtMore),
  263.                 Child, exitButton     = MakeButton(txtExit),
  264.             End,
  265.         End,
  266.         TAG_MORE, msg->ops_AttrList))
  267.     {
  268.         struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  269.         APTR parent;
  270.  
  271.         rwd->rwd_ResourceList = reslist;
  272.         rwd->rwd_ResourceText = restext;
  273.         rwd->rwd_ResourceCount = rescount;
  274.         rwd->rwd_RemoveButton = removeButton;
  275.         rwd->rwd_PriorityButton = priorityButton;
  276.         rwd->rwd_MoreButton = moreButton;
  277.         rwd->rwd_FunctionsButton = funcButton;
  278.  
  279.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  280.  
  281.         set(obj, MUIA_Window_Title, MyGetWindowTitle("RESOURCES", rwd->rwd_Title, sizeof(rwd->rwd_Title)));
  282.         set(obj, MUIA_Window_ActiveObject, reslist);
  283.         set(removeButton, MUIA_Disabled, TRUE);
  284.         set(priorityButton, MUIA_Disabled, TRUE);
  285.         set(moreButton, MUIA_Disabled, TRUE);
  286.         set(funcButton, MUIA_Disabled, TRUE);
  287.  
  288.         DoMethod(parent,         MUIM_Window_AddChildWindow, obj);
  289.         DoMethod(obj,            MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  290.         DoMethod(reslist,        MUIM_Notify, MUIA_NList_Active,        MUIV_EveryTime, obj,                     1, MUIM_ResourcesWin_ListChange);
  291.         DoMethod(reslist,        MUIM_Notify, MUIA_NList_DoubleClick,   MUIV_EveryTime, obj,                     1, MUIM_ResourcesWin_More);
  292.         DoMethod(updateButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_Update);
  293.         DoMethod(printButton,    MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_Print);
  294.         DoMethod(removeButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_Remove);
  295.         DoMethod(moreButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_More);
  296.         DoMethod(priorityButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_Priority);
  297.         DoMethod(funcButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_ResourcesWin_Functions);
  298.         DoMethod(exitButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  299.         DoMethod(reslist,        MUIM_NList_Sort3, MUIV_NList_Sort3_SortType_1, MUIV_NList_SortTypeAdd_None, MUIV_NList_Sort3_SortType_Both);
  300.     }
  301.  
  302.     return (ULONG)obj;
  303. }
  304.  
  305. static ULONG __saveds mDispose( struct IClass *cl,
  306.                                 Object *obj,
  307.                                 struct opSet *msg )
  308. {
  309.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  310.  
  311.     set(obj, MUIA_Window_Open, FALSE);
  312.     DoMethod(rwd->rwd_ResourceList, MUIM_NList_Clear);
  313.  
  314.     return (DoSuperMethodA(cl, obj, msg));
  315. }
  316.  
  317. static ULONG __saveds mUpdate( struct IClass *cl,
  318.                                Object *obj,
  319.                                Msg msg )
  320. {
  321.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  322.     struct ResourcesCallbackUserData ud;
  323.  
  324.     ApplicationSleep(TRUE);
  325.     set(rwd->rwd_ResourceList, MUIA_NList_Quiet, TRUE);
  326.     DoMethod(rwd->rwd_ResourceList, MUIM_NList_Clear);
  327.  
  328.     ud.ud_List = rwd->rwd_ResourceList;
  329.     ud.ud_Count = 0;
  330.  
  331.     if (clientstate) {
  332.         ReceiveList(UpdateCallback, &ud);
  333.     } else {
  334.         IterateList(UpdateCallback, &ud);
  335.     }
  336.  
  337.     SetCountText(rwd->rwd_ResourceCount, ud.ud_Count);
  338.     MySetContents(rwd->rwd_ResourceText, "");
  339.  
  340.     set(rwd->rwd_ResourceList, MUIA_NList_Quiet, FALSE);
  341.     set(rwd->rwd_PriorityButton, MUIA_Disabled, TRUE);
  342.     set(rwd->rwd_RemoveButton, MUIA_Disabled, TRUE);
  343.     set(rwd->rwd_MoreButton, MUIA_Disabled, TRUE);
  344.     set(rwd->rwd_FunctionsButton, MUIA_Disabled, TRUE);
  345.     ApplicationSleep(FALSE);
  346.  
  347.     return 0;
  348. }
  349.  
  350. static ULONG __saveds mPrint( struct IClass *cl,
  351.                               Object *obj,
  352.                               Msg msg )
  353. {
  354.     PrintResources(NULL);
  355.  
  356.     return 0;
  357. }
  358.  
  359. static ULONG __saveds mRemove( struct IClass *cl,
  360.                                Object *obj,
  361.                                Msg msg )
  362. {
  363.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  364.     struct ResourceEntry *re;
  365.  
  366.     if (re = (struct ResourceEntry *)GetActiveEntry(rwd->rwd_ResourceList)) {
  367.         if (MyRequest(msgYesNo, msgWantToRemoveResource, re->re_Name)) {
  368.             MyDoCommand("RemoveResource \"%s\"", re->re_Name);
  369.             DoMethod(obj, MUIM_ResourcesWin_Update);
  370.         }
  371.     }
  372.  
  373.     return 0;
  374. }
  375.  
  376. static ULONG __saveds mPriority( struct IClass *cl,
  377.                                  Object *obj,
  378.                                  Msg msg )
  379. {
  380.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  381.     struct ResourceEntry *re;
  382.  
  383.     if (re = (struct ResourceEntry *)GetActiveEntry(rwd->rwd_ResourceList)) {
  384.         LONG pri;
  385.  
  386.         pri = atol(re->re_Pri);
  387.         if (GetPriority(re->re_Name, &pri)) {
  388.             if (MyDoCommand("SetPriority RESOURCE \"%s\" %ld", re->re_Name, pri)) {
  389.                 _snprintf(re->re_Pri, sizeof(re->re_Pri), "%4ld", pri);
  390.                 RedrawActiveEntry(rwd->rwd_ResourceList);
  391.             }
  392.         }
  393.     }
  394.  
  395.     return 0;
  396. }
  397.  
  398. static ULONG __saveds mMore( struct IClass *cl,
  399.                              Object *obj,
  400.                              Msg msg )
  401. {
  402.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  403.     struct ResourceEntry *re;
  404.  
  405.     if (re = (struct ResourceEntry *)GetActiveEntry(rwd->rwd_ResourceList)) {
  406.         APTR detailWin;
  407.  
  408.         if (detailWin = ResourcesDetailWindowObject,
  409.                 MUIA_Window_ParentWindow, obj,
  410.                 MUIA_Window_MaxChildWindowCount, (opts.SingleWindows) ? 1 : 0,
  411.             End) {
  412.             set(detailWin, MUIA_ResourcesDetailWin_Resource, re);
  413.             set(detailWin, MUIA_Window_Open, TRUE);
  414.         }
  415.     }
  416.  
  417.     return 0;
  418. }
  419.  
  420. static ULONG __saveds mFunctions( struct IClass *cl,
  421.                                   Object *obj,
  422.                                   Msg msg )
  423. {
  424.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  425.     struct ResourceEntry *re;
  426.  
  427.     if (re = (struct ResourceEntry *)GetActiveEntry(rwd->rwd_ResourceList)) {
  428.         APTR funcWin;
  429.  
  430.         if (funcWin = FunctionsWindowObject,
  431.                 MUIA_Window_ParentWindow, obj,
  432.             End) {
  433.             DoMethod(funcWin, MUIM_FunctionsWin_ShowFunctions, MUIV_FunctionsWin_NodeType_Resource, re->re_Addr, re->re_Name);
  434.         }
  435.     }
  436.  
  437.     return 0;
  438. }
  439.  
  440. static ULONG __saveds mListChange( struct IClass *cl,
  441.                                    Object *obj,
  442.                                    Msg msg )
  443. {
  444.     struct ResourcesWinData *rwd = INST_DATA(cl, obj);
  445.     struct ResourceEntry *re;
  446.  
  447.     if (re = (struct ResourceEntry *)GetActiveEntry(rwd->rwd_ResourceList)) {
  448.         MySetContents(rwd->rwd_ResourceText, "%s \"%s\"", re->re_Address, re->re_Name);
  449.         set(rwd->rwd_PriorityButton, MUIA_Disabled, FALSE);
  450.         set(rwd->rwd_RemoveButton, MUIA_Disabled, FALSE);
  451.         if (!clientstate) {
  452.             set(rwd->rwd_MoreButton, MUIA_Disabled, FALSE);
  453.             set(rwd->rwd_FunctionsButton, MUIA_Disabled, FALSE);
  454.         }
  455.     }
  456.  
  457.     return 0;
  458. }
  459.  
  460. ULONG __asm __saveds ResourcesWinDispatcher( register __a0 struct IClass *cl,
  461.                                              register __a2 Object *obj,
  462.                                              register __a1 Msg msg )
  463. {
  464.     switch (msg->MethodID) {
  465.         case OM_NEW:                       return (mNew(cl, obj, (APTR)msg));
  466.         case OM_DISPOSE:                   return (mDispose(cl, obj, (APTR)msg));
  467.         case MUIM_ResourcesWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  468.         case MUIM_ResourcesWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  469.         case MUIM_ResourcesWin_Remove:     return (mRemove(cl, obj, (APTR)msg));
  470.         case MUIM_ResourcesWin_Priority:   return (mPriority(cl, obj, (APTR)msg));
  471.         case MUIM_ResourcesWin_More:       return (mMore(cl, obj, (APTR)msg));
  472.         case MUIM_ResourcesWin_Functions:  return (mFunctions(cl, obj, (APTR)msg));
  473.         case MUIM_ResourcesWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  474.     }
  475.  
  476.     return (DoSuperMethodA(cl, obj, msg));
  477. }
  478.  
  479. void PrintResources( char *filename )
  480. {
  481.     BPTR handle;
  482.  
  483.     if (handle = HandlePrintStart(filename)) {
  484.         PrintFOneLine(handle, "\n  Address   Pri OpenC RPC Name & Version\n\n");
  485.         IterateList(PrintCallback, (void *)handle);
  486.     }
  487.  
  488.     HandlePrintStop();
  489. }
  490.  
  491. void SendResList( void )
  492. {
  493.     IterateList(SendCallback, NULL);
  494. }
  495.  
  496.